import { deleteByPrimaryKeyForProUserOrgan,selectByPrimaryKeyForProUserOrgan, insertForProUserOrgan, updateForProUserOrgan, deleteByMapForProUserOrgan,updateByMapForProuserorgan, getOneForProUserOrgan,getAllForProUserOrgan,queryPageForProUserOrgan, countForProUserOrgan, insertBatchForProUserOrgan, deleteBatchForProUserOrgan,updateBatchForProUserOrgan,getProOrgTreeByLevelCode,getProOrgTreeByParentid,getPageProOrgByOrgCode,insertForProOrg,updateForProOrg,deleteByPrimaryKeyForProOrg,queryPageForProUser,updateChangeTargetNodeWithSourceNodeForProOrg } from '@/services/system/api_prouserorgan';

export default {
    namespace: 'prouserorgan',

    state: {
        data: {
            list: [],
            pagination: {},
        },
        organTree:{
        	list: []
        },
        recorddata: {
            datarecord:{},
            message:null,
            success: false,
        },
    },

    effects: {
            *delete_by_primarykey_for_prouserorgan({ payload }, { call, put }) {
                const response = yield call(deleteByPrimaryKeyForProUserorgan, payload);
                yield put({
                    type: 'deleteByPrimaryKeyForProuserorgan',
                    payload: response,
                });
            },
            *select_by_primarykey_for_prouserorgan({ payload, callback }, { call, put }) {
                const response = yield call(selectByPrimaryKeyForProUserOrgan, payload);
                yield put({
                    type: 'selectByPrimaryKeyForProuserorgan',
                    payload: response,
                });
                if (callback) callback();
            },
            *insert_for_prouserorgan({ payload, callback }, { call, put }) {
                const response = yield call(insertForProUserOrgan, payload);
                yield put({
                    type: 'insertForProuserorgan',
                    payload: response,
                });
                if (callback) callback();
            },
            *update_for_prouserorgan({ payload, callback }, { call, put }) {
                const response = yield call(updateForProUserOrgan, payload);
                yield put({
                    type: 'updateForProuserorgan',
                    payload: response,
                });
                if (callback) callback();
            },
            *delete_by_map_for_prouserorgan({ payload, callback }, { call, put }) {
                const response = yield call(deleteByMapForProUserOrgan, payload);
                yield put({
                    type: 'deleteByMapForProuserorgan',
                    payload: response,
                });
                if (callback) callback();
            },
            *update_by_map_for_prouserorgan({ payload, callback }, { call, put }) {
                const response = yield call(updateByMapForProUserOrgan, payload);
                yield put({
                    type: 'updateByMapForProuserorgan',
                    payload: response,
                });
                if (callback) callback();
            },
            *get_one_for_prouserorgan({ payload, callback }, { call, put }) {
                const response = yield call(getOneForProUserOrgan, payload);
                yield put({
                    type: 'getOneForProuserorgan',
                    payload: response,
                });
                if (callback) callback();
            },
            *get_all_for_prouserorgan({ payload, callback }, { call, put }) {
                const response = yield call(getAllForProUserOrgan, payload);
                yield put({
                    type: 'getAllForProuserorgan',
                    payload: response,
                });
                if (callback) callback();
            },
            *query_page_for_prouserorgan({ payload, callback }, { call, put }) {
                const response = yield call(queryPageForProUserOrgan, payload);
                yield put({
                    type: 'queryPageForProuserorgan',
                    payload: response,
                });
                if (callback) callback();
            },
            *count_for_prouserorgan({ payload, callback }, { call, put }) {
                const response = yield call(countForProUserOrgan, payload);
                yield put({
                    type: 'countForProuserorgan',
                    payload: response,
                });
                if (callback) callback();
            },
            *insert_batch_for_prouserorgan({ payload, callback }, { call, put }) {
                const response = yield call(insertBatchForProUserOrgan, payload);
                yield put({
                    type: 'insertBatchForProuserorgan',
                    payload: response,
                });
                if (callback) callback();
            },
            *delete_batch_for_prouserorgan({ payload, callback }, { call, put }) {
                const response = yield call(deleteBatchForProUserOrgan, payload);
                yield put({
                    type: 'deleteBatchForProuserorgan',
                    payload: response,
                });
                if (callback) callback();
            },
            *update_batch_for_prouserorgan({ payload, callback }, { call, put }) {
                const response = yield call(updateBatchForProUserOrgan, payload);
                yield put({
                    type: 'updateBatchForProuserorgan',
                    payload: response,
                });
                if (callback) callback();
            },
            
            *get_proorgtree_by_levelcode({ payload, callback }, { call, put }) {
                const response = yield call(getProOrgTreeByLevelCode, payload);
                yield put({
                    type: 'getProOrgTreeByLevelCode',
                    payload: response,
                });
                if (callback) callback();
            },
            *get_proorgtree_by_parentid({ payload, callback }, { call, put }) {
                const response = yield call(getProOrgTreeByParentid, payload);
                yield put({
                    type: 'getProOrgTreeByParentid',
                    payload: response,
                });
                if (callback) callback();
            },
            *get_page_proorg_by_orgcode({ payload, callback }, { call, put }) {
                const response = yield call(getPageProOrgByOrgCode, payload);
                yield put({
                    type: 'getPageProOrgByOrgCode',
                    payload: response,
                });
                if (callback) callback();
            },
            *insert_for_proorg({ payload, callback }, { call, put }) {
                const response = yield call(insertForProOrg, payload);
                yield put({
                    type: 'insertForProOrg',
                    payload: response,
                });
                
                const newPayload = {
                	parentid: payload.parent_id,
                };
                
                const responseData = yield call(getProOrgTreeByParentid, newPayload);
                yield put({
                    type: 'getProOrgTreeByParentid',
                    payload: responseData,
                });
                const responseUser = yield call(queryPageForProUser, {});
                yield put({
                    type: 'queryPageForProUser',
                    payload: responseUser,
                });
                if (callback) callback();
            },
            *update_for_proorg({ payload, callback }, { call, put }) {
                const response = yield call(updateForProOrg, payload);
                yield put({
                    type: 'updateForProOrg',
                    payload: response,
                });
                                
                const newPayload = {
                	parentid: payload.parent_id,
                };            
                const responseData = yield call(getProOrgTreeByParentid, newPayload);
                yield put({
                    type: 'getProOrgTreeByParentid',
                    payload: responseData,
                });
                const responseUser = yield call(queryPageForProUser, {});
                yield put({
                    type: 'queryPageForProUser',
                    payload: responseUser,
                });
                if (callback) callback();
            },
            *delete_by_primarykey_for_proorg({ payload, callback }, { call, put }) {
                const response = yield call(deleteByPrimaryKeyForProOrg, payload);
                yield put({
                    type: 'deleteByPrimaryKeyForProOrg',
                    payload: response,
                });
                
                const newPayload = {
                	parentid: payload.parent_id,
                };
                
                const responseData = yield call(getProOrgTreeByParentid, newPayload);
                yield put({
                    type: 'getProOrgTreeByParentid',
                    payload: responseData,
                });
                
                const responseUser = yield call(queryPageForProUser, {});
                yield put({
                    type: 'queryPageForProUser',
                    payload: responseUser,
                });
                if (callback) callback();
            },
            *update_change_target_node_with_source_node_pro_org({ payload, callback }, { call, put }) {
                const response = yield call(updateChangeTargetNodeWithSourceNodeForProOrg, payload);
                yield put({
                    type: 'updateChangeTargetNodeWithSourceNodeForProOrg',
                    payload: response,
                });
                
        

                if (callback) callback(response);
            },
            },

    reducers: {
        deleteByPrimaryKeyForProuserorgan(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        selectByPrimaryKeyForProuserorgan(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        insertForProuserorgan(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        updateForProuserorgan(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        deleteByMapForProuserorgan(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        updateByMapForProuserorgan(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        getOneForProuserorgan(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        getAllForProuserorgan(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        queryPageForProuserorgan(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        countForProuserorgan(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        insertBatchForProuserorgan(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        deleteBatchForProuserorgan(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        updateBatchForProuserorgan(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        getProOrgTreeByLevelCode(state, action) {
            return {
                ...state,
                organTree: action.payload,
            };
        },
        getProOrgTreeByParentid(state, action) {
            return {
                ...state,
                organTree: action.payload,
            };
        },
        getPageProOrgByOrgCode(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        insertForProOrg(state, action) {
            return {
                ...state,
                recorddata: action.payload,
            };
        },
        updateForProOrg(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        deleteByPrimaryKeyForProOrg(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        queryPageForProUser(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        updateChangeTargetNodeWithSourceNodeForProOrg(state, action) {
            return {
                ...state,
                recorddata: action.payload,
            };
        },
    },
};
